/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.view.security;

import at.irian.demo.jsfatwork.domain.User;
import at.irian.demo.jsfatwork.service.UserService;
import at.irian.demo.jsfatwork.view.ConversationScoped;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.orchestra.conversation.*;
import org.springframework.context.annotation.Bean;

import javax.inject.Inject;

/**
 * @author Gerhard Petracek
 */
@ConversationScoped
@SuppressWarnings({"SpringJavaConfigInconsistencyInspection"})
public class UserHolder implements ConversationBindingListener, ConversationAware
{
    private final static Log LOGGER = LogFactory.getLog(UserHolder.class);

    private String currentUserName;

    @Inject
    private UserService userService;

    private Conversation conversation;

    /**
     * This method will get called only one time per conversation as this method returns
     * a conversation scoped user object.
     * @return the conversation scoped user object
     */
    @Bean(name = "currentUser")
    @ConversationScoped
    public User loadUser()
    {
        if (this.currentUserName != null)
        {
            User user = this.userService.loadUser(this.currentUserName);

            return user != null ? user : this.userService.createNew();
        }
        return new User();
    }

    public String getCurrentUserName()
    {
        return currentUserName;
    }

    public void setCurrentUserName(String currentUserName)
    {
        this.currentUserName = currentUserName;
    }

    public void valueBound(ConversationBindingEvent conversationBindingEvent)
    {
        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("[bind] user holder");
        }
    }

    public void valueUnbound(ConversationBindingEvent conversationBindingEvent)
    {
        if (LOGGER.isDebugEnabled())
        {
            LOGGER.debug("[un-bind] holder bound");
        }
    }

    public void reset()
    {
        this.conversation.invalidate();
    }

    public void setConversation(Conversation conversation)
    {
        this.conversation = conversation;
    }

    public String logout()
    {
        ConversationManager.getInstance().clearCurrentConversationContext();
        return "pages/login";
    }
}